/**
 * Created by meiguizhinian on 2018/7/23
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIShrinkGroup = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();
		this._childControlsPosVec = [];
		this._childShrinkDataVec = [];
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith : function (formName, blockName, sourceControlData) {
		this.init();
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		this.setAnchorType(ivy.UIBase.UIAnchorType.eAnchorHV);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_OutAction_action]));
		this.setUIElementName(formName, blockName);

		let controlCheckBoxName = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_ControlCheckBoxName_string]);
		let childControlInfoStr = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_ChildControlNamesString_string]);
		let linkedSysvalIndex = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_linkedSystemValIndex_int]);
		let childMoveTime = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_ChildControlsMoveTime_float]);
		let openStartPosElementName = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ShrinkGroup_NewControlStartPosControl_string]);
		this._childActionTime = childMoveTime;
		let vOpenRect = ivy.UIManager.getInstance().getBlockRect(formName, openStartPosElementName);
		let vOpenMiddlePos = ivy.Tools.ConvertPointToGL(cc.p(cc.rectGetMidX(vOpenRect), cc.rectGetMidY(vOpenRect)));

		if (linkedSysvalIndex >= 0) {
			this._getCheckValueFunc = function() {
				return ivy.EditorDataManager.getInstance().getSystemVariateValue(linkedSysvalIndex);
			};
		}

		ivy.Tools.pushFunctionToCocosThread(function(){
			this.delayInitBy(controlCheckBoxName, childControlInfoStr);
			this._openPos = this.convertToNodeSpace(vOpenMiddlePos);
		}.bind(this));
		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
		this.scheduleUpdate();
	},


	delayInitBy : function(controlCheckBoxName, childControlInfoStr) {
		let parentBaseObj = this.getParent();
		if (parentBaseObj instanceof ivy.UIBase) {
			let cbControl = parentBaseObj.getChildByName(controlCheckBoxName, ivy.UICheckBox);
			if (cbControl) {
				this._controlCheckBox = cbControl;
				this._controlCheckBox.onCheckChanged().add(function(obj){
					this.doChildShrinkLogic(obj.isChecked());
				}.bind(this));
				let infoVec = [] = childControlInfoStr.split("|");
				cc.assert(infoVec.length%3===0);
				for (let i = 0; i < infoVec.length; i+=3) {
					let childName = infoVec[i];
					let controlV = Number(infoVec[i + 1]);
					let pos = Number(infoVec[i + 2]);
					let data = new ivy.UIShrinkGroup.ShrinkChildData();
					data.controlElement = this.getChildByName(childName);
					data.checkValue = controlV;
					data.pos = pos;
					this._childShrinkDataVec.push(data);
					if (!data.controlElement) {
						return;
					} else {
						data.controlElement.setVisible(false);
					}
				}
				this._childShrinkDataVec.sort(function (v1, v2) {
					return v1.pos-v2.pos;
				});

				for (let v of this._childShrinkDataVec) {
					this._childControlsPosVec.push(v.controlElement.getSrcPosition());
				}
				this._dataInitFinish = true;
			}
		}
	},
	setCheckValueFunc : function(func) {
		this._getCheckValueFunc = func;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	doChildShrinkLogic : function(show, firstTime=false) {
		if (this._controlCheckBox) {
			let worldPos = this._controlCheckBox.convertToWorldSpace(cc.p());
			let destPos = this.convertToNodeSpace(worldPos);
			this._controlCheckBox.runAction(cc.sequence(
				cc.callFunc(function() {this._controlCheckBox.setEnableUserTouch(false); }.bind(this)),
				cc.delayTime(this._childActionTime),
				cc.callFunc(function() {this._controlCheckBox.setEnableUserTouch(true); }.bind(this))
			));
			if (show) {
				let newVec = [];
				for (let data of this._childShrinkDataVec) {
					if (data.checkValue <= this._lastCheckV) {
						newVec.push(data);
					}
				}
				newVec.sort(function (a, b) {
					return a.pos-b.pos;
				});
				let index = 0;
				for (let data of newVec) {
					data.controlElement.setPosition(destPos);
					data.controlElement.runAction(cc.sequence(
						cc.callFunc(function() {data.controlElement.setEnableUserTouch(false); }),
						cc.show(),
						cc.moveTo(this._childActionTime, this._childControlsPosVec[index]).easing(cc.easeSineOut()),
						cc.callFunc(function() {data.controlElement.setEnableUserTouch(true); })
					));
					data.controlElement.setVisible(true);
					++index;
				}
			} else {
				let newVec = [];
				for (let data of this._childShrinkDataVec) {
					if (data.checkValue <= this._lastCheckV) {
						newVec.push(data);
					}
				}
				newVec.sort(function (a, b) {
					return a.pos-b.pos;
				});
				let index = 0;
				for (let data of newVec) {
					data.controlElement.setPosition(this._childControlsPosVec[index]);
					if (firstTime) {
						data.controlElement.runAction(cc.sequence(
							cc.callFunc(function() {data.controlElement.setEnableUserTouch(false); }),
							cc.hide(),
							cc.moveTo(this._childActionTime, destPos).easing(cc.easeSineOut()),
							cc.callFunc(function() {data.controlElement.setEnableUserTouch(true); })
						));
					} else {
						data.controlElement.runAction(cc.sequence(
							cc.callFunc(function() {data.controlElement.setEnableUserTouch(false); }),
							cc.moveTo(this._childActionTime, destPos).easing(cc.easeSineOut()),
							cc.hide(),
							cc.callFunc(function() {data.controlElement.setEnableUserTouch(true); })
						));
					}

					data.controlElement.setVisible(true);
					++index;
				}
			}
		}
	},
	firstShowChild : function() {
		let newVec = [];
		for (let data of this._childShrinkDataVec) {
			if (data.checkValue <= this._lastCheckV) {
				newVec.push(data);
			}
		}
		newVec.sort(function (a,b) {
			return a.pos - b.pos;
		});
		let index = 0;
		for (let data of newVec) {
			data.controlElement.setPosition(this._childControlsPosVec[index]);
			data.controlElement.setVisible(true);
			++index;
		}
	},
	showChildInUpdate : function(refreshAllControl=false) {
		if(this._controlCheckBox)
			this._controlCheckBox.runAction(cc.sequence(
				cc.callFunc(function () {this._controlCheckBox.setEnableUserTouch(false)}.bind(this)),
				cc.delayTime(this._childActionTime),
				cc.callFunc(function () {this._controlCheckBox.setEnableUserTouch(true)}.bind(this))
			));

		let newVec = [];
		for (let data of this._childShrinkDataVec) {
			if (data.checkValue <= this._lastCheckV) {
				newVec.push(data);
			}
		}
		newVec.sort(function (v1, v2) {
			return v1.pos - v2.pos;
		});
		let index = 0;
		for (let data of newVec) {
			if (!data.controlElement.isVisible()) {
				data.controlElement.setPosition(this._openPos);
				data.controlElement.runAction(cc.MoveTo(this._childActionTime, this._childControlsPosVec[index]).easing(cc.easeSineOut()));
			} else {
				data.controlElement.runAction(cc.MoveTo(this._childActionTime, this._childControlsPosVec[index]).easing(cc.easeBounceOut()));
			}
			if (refreshAllControl) {
				data.controlElement.runAction(cc.MoveTo(this._childActionTime, this._childControlsPosVec[index]).easing(cc.easeBounceOut()));
			}
			data.controlElement.setVisible(true);
			++index;
		}
	},
	update : function(dt) {
		if (this._dataInitFinish) {
			if (this._getCheckValueFunc) {
				let currentCheckV = this._getCheckValueFunc();
				//初次初始化
				if (this._lastCheckV < 0 && this._lastCheckV !== currentCheckV) {
					this._lastCheckV = currentCheckV;
					this.firstShowChild();
					if (this._controlCheckBox) {
						this.doChildShrinkLogic(this._controlCheckBox.isChecked(),true);
					}
				} else if(this._lastCheckV !== currentCheckV) {
					this._lastCheckV = currentCheckV;
					this.showChildInUpdate();
				}
			}
		}
	},
	////////////////////////////////////////////////////////////////////////////
	_controlCheckBox : null,//cc::UICheckBox*
	_childControlsPosVec : null,//std::vector<cocos2d::Vec2>
	_childShrinkDataVec : null,//std::vector<ShrinkChildData>
	_dataInitFinish : false,
	_lastCheckV : -1,
	_childActionTime : 0.0,
	_openPos : null,//cocos2d::Vec2
	_getCheckValueFunc : null,//std::function<int32_t()>
});

ivy.UIShrinkGroup.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIShrinkGroup(formName, blockName, sourceControlData);
};


(function(){
	ivy.UIShrinkGroup.ShrinkChildData = function (param) {
		//成员变量
		this.controlElement = null;
		this.checkValue = 0;
		this.pos = 0;
	};
})();
